
    #include <iostream>
    #include <fstream>
    #include <cmath>
    #include <sstream>
    using namespace std;

    void usage(){

        cout << "usage: "<< endl;
        cout << "analizer <time> <raio>" << endl;

    }

    int main(int argc, const char *argv[])
    {
        double n,R,X,Y,time,raio;
        double x,y,r,v;
        double num_nodes_1_quadrante=0;
        double num_nodes_2_quadrante=0;
        double num_nodes_3_quadrante=0;
        double num_nodes_4_quadrante=0;
        double num_nodes_coroa_interna=0;
        double num_nodes_coroa_externa=0;

        if(argc==3){
            time=atof(argv[1]);
            raio=atof(argv[2]);
        }else{
            usage();
            return EXIT_SUCCESS;
        }

        ifstream init;
        init.open("inicio");
        init >>n>>R>>X>>Y;
        init.close();

        double nodes[4][(int)n];
        int vizinhos[(int)time][(int)n];

        for (int t=0;t<time;t++)
            for(int i=0;i<n;i++)
                vizinhos[t][i] = 0;

        ofstream output;
        output.open("output.m");
        output << "velocidades = ["<<endl;
        ifstream time_file;
        for(int t=0;t<time;t++){
            char name_file_time[30] = "time";
            char num[30];
            itoa(t,num,10);
            strcat(name_file_time,num);
            time_file.open(name_file_time);
            for(int i=0;i<n;i++){
                time_file >>x>>y>>r>>v;
                nodes[0][i] = x;
                nodes[1][i] = y;
                nodes[2][i] = r;
                nodes[3][i] = v;

            }
            time_file.close();
            for(int i=0;i<n;i++){
                x = nodes[0][i];
                y = nodes[1][i];
                r = nodes[2][i];
                v = nodes[3][i];

                output << v <<" " ;
                if (x > X/2.0 && y>Y/2.0)
                    num_nodes_1_quadrante++;

                if (x<X/2.0 && y>Y/2.0)
                    num_nodes_2_quadrante++;

                if (x<X/2.0 && y<Y/2.0)
                    num_nodes_3_quadrante++;

                if (x>X/2.0 && y<Y/2.0)
                    num_nodes_4_quadrante++;

                if (r>0.0 && r<R/sqrt(2.0))
                    num_nodes_coroa_interna++;
                else
                    num_nodes_coroa_externa++;

                for(int k=0;k<n;k++){
                    if(k!=i){
                        x = nodes[0][k] - nodes[0][i];
                        y = nodes[1][k] - nodes[1][i];
                        r = sqrt(pow(x,2.0) + pow(y,2.0));
                        if (r<=raio)
                            vizinhos[t][k]++;
                    }
                }

            }
            output << ";" <<endl;
            cout << t << endl;
        }

        output << "];"<<endl;
        //saida padrao
        cout << "Output:" <<endl;
        cout << ".................................................."<<endl;
        cout << "nodes 1 quadrante: " <<num_nodes_1_quadrante/time<<endl;
        cout << "nodes 2 quadrante: " <<num_nodes_2_quadrante/time<<endl;
        cout << "nodes 3 quadrante: " <<num_nodes_3_quadrante/time<<endl;
        cout << "nodes 4 quadrante: " <<num_nodes_4_quadrante/time<<endl;
        cout << "nodes coroa interna: " <<num_nodes_coroa_interna/time<<endl;
        cout << "nodes coroa externa: " <<num_nodes_coroa_externa/time<<endl;
        cout << "media vizinhos <raio="<< raio<<">: "<<endl;
        //saida arquivo
        output << "num_nodes_1_quadrante = " <<num_nodes_1_quadrante/time<<";"<<endl;
        output << "num_nodes_2_quadrante = " <<num_nodes_2_quadrante/time<<";"<<endl;
        output << "num_nodes_3_quadrante = " <<num_nodes_3_quadrante/time<<";"<<endl;
        output << "num_nodes_4_quadrante = " <<num_nodes_4_quadrante/time<<";"<<endl;
        output << "num_nodes_coroa_interna =" <<num_nodes_coroa_interna/time<<";"<<endl;
        output << "num_nodes_coroa_externa = " <<num_nodes_coroa_externa/time<<";"<<endl;
        output << "vizinhos = [" <<endl;

        double soma;
        for(int i=0;i<n;i++){
            soma=0.0;
            for(int t=0;t<time;t++){
                soma = soma + vizinhos[t][i]/n;
                output << vizinhos[t][i]/n << " ";
            }
            cout << "no("<<i<<") = " <<(soma/time)<<endl;
            output << ";"<<endl;
        }
        cout << ".................................................."<<endl;

        output << "];"<<endl;

        output.close() ;

        return EXIT_SUCCESS;
    }
